/**
 * COPYRIGHT© KMTC 2007
 * This software is the proprietary information of Korea Marine Transport Co.LTD
 * 
 * Revision History
 * Author			Date		Description
 * ------			----		-----------
 * esther			08.01.10	
 */
package kr.or.javacafe.common.mail;

import java.util.*;
import java.io.*;
import javax.activation.*; 
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;

public class HtmlMimeMessagePreparator implements MimeMessagePreparator {


	private String htmlTemplate;
	private String from;
	private String to;
	private String subject;
	private Map data;
	private String fileTempDir;



	public synchronized void setHtmlTemplate(String htmlTemplate) {
		this.htmlTemplate = htmlTemplate;
	}

	public synchronized void setFrom(String from) {
		this.from = from;
	}

	public synchronized void setTo(String to) {
		this.to = to;
	}

	public synchronized void setSubject(String subject) {
		this.subject = subject;
	}

	public synchronized void setData(Map data) {
		this.data = data;
	}

	public synchronized void setFileTempDir(String fileTempDir) {
		this.fileTempDir = fileTempDir;
	}
	
	public void prepare(MimeMessage mimeMessage) throws Exception {
		// set header details
		mimeMessage.addFrom(InternetAddress.parse(from));

		String[] tempArray = to.split("\\;");
		if (tempArray != null && tempArray.length > 0) { // substitu . for ;
			String temp = "";
			for (int i = 0; i < tempArray.length; i++) {
				temp += tempArray[i] + ",";
			}

			to = temp.substring(0, temp.length() - 1);
		}
		mimeMessage.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
		mimeMessage.setSubject(subject);

		// create wrapper multipart/alternative part
		MimeMultipart ma = new MimeMultipart("alternative");
		mimeMessage.setContent(ma);

		// create the html part
		
		String msg="";

        String line=null;

        FileReader fr = new FileReader(fileTempDir+"/sailSchedule.jsp");

        BufferedReader br = new BufferedReader(fr);

        while ((line=br.readLine())!=null) {

            msg+=line;

        }
		BodyPart html = new MimeBodyPart();
		html.setContent(msg, "text/html; charset=UTF-8");
		ma.addBodyPart(html);
	}

}

